﻿package levels {
	import flash.display.*;
	import flash.events.*;
	import flash.utils.getDefinitionByName;
	import org.flixel.*;
	import levels.states.*;
	import external.*;
	import sprites.Fire;
	import sprites.LevelObject;
	import sprites.Sheep;
	import sprites.Sheeps;
	import states.LevelState;

	/**
	 * ...
	 * @author test
	 */
	public class Level extends Sprite {
		//public var _TileGraphic:FlxExtSprite;
		public var sheeps:Sheeps;
		public var trees:uint;
		public var _groups:Array;
		public var _obj_group:FlxGroup;
		public var _obj_group_index:uint;
		public var _fire_obj_index:Array;
		public var _objects:Array;
		public var _csv_ary:Array;
		public var _id:String;
		public var _start:LevelObject;
		public var _exit:LevelObject;
		public var _state:LevelState;
		[Embed(source="../../bin/imgs/sets/tile_ground.png")]
		public var _TileGraphic:Class;
		[Embed(source="../../bin/imgs/door.png")]
		public var _ExitGraphic:Class;
		[Embed(source="../../bin/imgs/door.png")]
		public var _StartGraphic:Class;

		public function Level(id:String, TMXUrl:String){
			_id = id;
			_csv_ary = new Array();
			_groups = new Array();
			_fire_obj_index = new Array();
			_groups["collide"] = new Array(); //For layers which are set for walls
			_objects = new Array();
			trees = new uint(0);
			sheeps = new Sheeps();

			var preStates:preState = new preState();
			var c:Class = getDefinitionByName("levels.states." + id + "State") as Class;
			_state = new c;

			loadTMX(TMXUrl);
		}

		protected function loadTMX(url:String):void {
			var tmx:TMX = new TMX(url, _csv_ary, _TileGraphic, _groups, _objects);
			tmx.addEventListener("TMXLoadComp", TMXLoadComp);
		}

		protected function TMXLoadComp(evt:Event):void {
			evt.target.removeEventListener("TMXLoadComp", TMXLoadComp);

			//Handle CSVs
			for each (var csv:String in _csv_ary){
				//trace(csv);
				var tmp_tilemap:FlxTilemap = new FlxTilemap();
				tmp_tilemap.loadMap(csv, _TileGraphic);

				var tmp_group:FlxGroup = new FlxGroup();
				tmp_group.add(tmp_tilemap);
				_groups.push(tmp_group);
			}

			//Handle LevelObjects
			//var obj_group:FlxGroup = new FlxGroup();
			_obj_group = new FlxGroup();
			for each (var obj:*in _objects) {
				var dont_add:Boolean = new Boolean(false);
				
				if (obj.name == "start"){
					_start = obj;
					_start.x += _start.size.x / 2;
					_start.x += _start.size.y / 2;
					_start.loadGraphic(_StartGraphic);
				} else if (obj.name == "exit"){
					_exit = obj;
					_exit.x += _exit.size.x / 2;
					_exit.x += _exit.size.y / 2;
					_exit.loadGraphic(_ExitGraphic);
				} else if (obj.name == "fire"){
					var tmp_fire:Fire = new Fire(_groups[_groups["collide"][0]].members[0], obj.x, obj.y);

					obj = tmp_fire;
					_fire_obj_index.push(_obj_group.members.length);
						//trace(obj.fires[0].x, obj.fires[0].y);
				} else if (obj.name == "sheep"){
					var tmp_sheep:Sheep = new Sheep(obj.x, obj.y);
					sheeps.add_sheep(tmp_sheep);
					dont_add = true;
				}

				if (!dont_add){
					_obj_group.add(obj);
				}
			}
			_groups.push(_obj_group);
			_obj_group_index = new uint(_groups.length - 1);

			trace("save++");
			FlxG.save++;

			this._state.load(_id);
		}
	/*
	   protected function loadExtTileGraphic(url:String):void {
	   var loader:Loader = new Loader();
	   loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadExtTileGraphic_loader_handler);
	   loader.load(new URLRequest(url));
	   }

	   protected function loadExtTileGraphic_loader_handler(evt:Event):void {

	   _TileGraphic = new FlxExtSprite(0, 0);
	   _TileGraphic.loadExtGraphic(new Bitmap(evt.target.content.bitmapData));

	   }
	 */
	}

}